<?php
  include('header.php');
  
  $hide_footer = -1;
  $counter = 0;
  $qqq = 1;
  $pageTitle = "$l_view_unnanswered";
  $sort = "DESC";
  $bys = "post_date";
  
  $userid = -5;
  if (isset($_SESSION['user_id']))
  {$userid = $_SESSION['user_id'];}
  
  if (isset($_GET['sort']))
  {
  if (mysql_real_escape_string($_GET['sort']) == "desc") {
      $sort = "DESC";
      $bys = "post_date";
  }
  if (mysql_real_escape_string($_GET['sort']) == "asc") {
      $sort = "ASC";
      $bys = "post_date";
  }
  if (mysql_real_escape_string($_GET['sort']) == "sub") {
      $sort = "ASC";
      $bys = "post_subject";
  }
  }
?>
<!--<br />Sort by:
<select class="dropdown" name="sort" ONCHANGE="location = this.options[this.selectedIndex].value;">
<option value="view_new.php?sort=desc">-----</option>
<option value="view_new.php?sort=desc">Descending</option>
<option value="view_new.php?sort=asc" >Ascending</option>
<option value="view_new.php?sort=sub" >Post Subject</option>
</select><br />
-->
<?php
  echo "<b>$l_view_unnanswered</b><hr/>";
  $check_approved = "AND topic_status <= 1";
			  if ($_SESSION['user_level']>=1)
			  {$check_approved = "";}
  $result = $db->query("SELECT COUNT(*) FROM " . $table_prefix . "topics WHERE reply = 0 $check_approved");
  if (!$result) {
      echo "An error occured, please try again later.";
  } else {
      $r = $db->fetch_row($result);
      $numrows = $r[0];
      $rowsperpage = $max_search_results;
      $totalpages = ceil($numrows / $rowsperpage);
      if ($totalpages == 0) {
          $totalpages = 1;
      }
      echo "<table><tr>";
      if (isset($_GET['page']) && is_numeric($_GET['page'])) {
          $currentpage = (int)$_GET['page'];
      } else {
          $currentpage = 1;
      }
      if ($currentpage > $totalpages) {
          $currentpage = $totalpages;
      }
      if ($currentpage < 1) {
          $currentpage = 1;
      }
      $offset = ($currentpage - 1) * $rowsperpage;
      $current_page = $page;
      if ($current_page == "") {
          $current_page = 1;
      }
      $range = $max_pagination_link;
      if ($range >= $totalpages) {
          $range = $totalpages;
      }
      echo '<td align="right" class="pagination"> <b>' . $l_page . ' ' . $current_page . ' ' . $l_page_of . ' ' . $totalpages . '</b> [' . $numrows . ' maches]  &nbsp; &nbsp;';
      if ($currentpage > 1) {
          echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?page=1'><<</a> ";
          $prevpage = $currentpage - 1;
          echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?page=$prevpage'><</a> ";
      }
      for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
          if (($x > 0) && ($x <= $totalpages)) {
              if ($x == $currentpage) {
                  echo " [<b>$x</b>] ";
              } else {
                  echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
              }
          }
      }
      if ($currentpage != $totalpages) {
          $nextpage = $currentpage + 1;
          echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?page=$nextpage'>></a> ";
          echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?page=$totalpages'>>></a> ";
      }
      echo '</td></tr></table>';
      if ($page <= 0 or $page > $totalpages) {
          $page = 1;
      }
      
      $offset = $max_search_results * ($page - 1);
      $qqq += 1;
      
      $result2 = $db->query("SELECT * FROM " . $table_prefix . "topics WHERE reply = 0 $check_approved ORDER BY topic_sort,topic_last_post_date DESC LIMIT $offset, $max_search_results");
      if (!$result2) {
          echo "An error occured!";
          die;
      }
      $qqq += 1;
      
      $cat_icon = '*';
      
      $tags = array('{ICON}', '{TOPIC}', '{REPLIES}', '{VIEWS}', '{CREATED}', );
      $data = array($cat_icon, $l_topic, $l_replies, $l_views, $l_created);
      echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/topics.html"));
      
      while ($list = $db->fetch_array($result2,'assoc')) {
          $result44 = $db->query("SELECT cat_permission FROM " . $table_prefix . "categories WHERE cat_id = " . $list['topic_cat'] . "");
          $row44 = $db->fetch_array($result44,'assoc');
          
          if ($row44['cat_permission'] == 3 and $_SESSION['signed_in'] and $_SESSION['user_level'] >= 1) {
              $hide_forum = 0;
          }
          if ($row44['cat_permission'] == 3 and $_SESSION['signed_in'] and $_SESSION['user_level'] < 1) {
              $hide_forum = 1;
          }
          if ($row44['cat_permission'] == 3 and $_SESSION['signed_in'] == false) {
              $hide_forum = 1;
          }
          if ($row44['cat_permission'] == 2 and $_SESSION['signed_in'] == false) {
              $hide_forum = 1;
          }
          if ($row44['cat_permission'] == 2 and $_SESSION['signed_in']) {
              $hide_forum = 0;
          }
          
          if ($row44['cat_permission'] <= 1) {
              $hide_forum = 0;
          }
          
          $pages = ceil(($list['reply']) / $post_per_page);
          if ($pages == 0) {
              $pages = 1;
          }
          
          if ($hide_forum != 1) {
              $last_poster = "<a class='last_poster' href = 'user.php?u=" . $list['topic_last_poster_id'] . "'>" . $list['topic_last_poster'] . "</a>";
              if ($list['topic_last_poster_id'] <= 1) {
                  $last_poster = "<i>" . $list['topic_last_poster'] . "</i>";
              }
              //TOPIC ICONS
              $topic_icon = "./style/" . $default_style . "/img/normal_post.gif";
              $title_icon = "$l_tnormal";
              
              if ($list['reply'] >= $hot_topics_post) {
                  $topic_icon = "./style/" . $default_style . "/img/topic_hot.gif";
                  $title_icon = "$l_thot";
              }
              
              if ($list['topic_by'] == $userid) {
                  $topic_icon = "./style/" . $default_style . "/img/my_topic.gif";
                  $title_icon = "$l_tyou";
              }
              
              if ($list['topic_by'] == $userid and $list['reply'] >= $hot_topics_post) {
                  $topic_icon = "./style/" . $default_style . "/img/my_hot_topic.gif";
                  $title_icon = "$l_thotyou";
              }
              
              if ($list['topic_status'] == 1) {
                  $topic_icon = "./style/" . $default_style . "/img/topic_locked.gif";
                  $title_icon = "$l_tlocked";
              }
              $latest = "./style/" . $default_style . "/img/";
              
              if ($list['topic_sort'] == -1) {
                  $topic_icon = "./style/" . $default_style . "/img/quick_sticky.gif";
                  $title_icon = "$l_tsticky";
              }
              $latest = "./style/" . $default_style . "/img/";
              
              if ($list['topic_sort'] == -1 and $list['topic_status'] == 1) {
                  $topic_icon = "./style/" . $default_style . "/img/normal_post_locked_sticky.gif";
                  $title_icon = "$l_tlockedsticky";
              }
              $latest = "./style/" . $default_style . "/img/";
              
              $tpages = ceil(($list['reply'] + 1) / $post_per_page);
              if ($tpages <= 1) {
                  $topic_pages = "";
              } else {
                  $topic_pages = " (<img src = './style/$default_style/img/more_pages_view.gif'> <a class='underline' href = 'topic.php?t=$list[topic_id]&page=1'>1</a>";
                  $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=2'>2</a>";
                  if ($tpages > 2) {
                      $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=3'>3</a>";
                  }
                  if ($tpages > 3) {
                      $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=4'>4</a>";
                  }
                  if ($tpages > 4) {
                      $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=5'>5</a>";
                  }
                  if ($tpages > 5) {
                      $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=6'>6</a>";
                  }
                  if ($tpages > 6) {
                      $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=7'>7</a>";
                  }
                  if ($tpages > 7) {
                      $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=8'>8</a>";
                  }
                  
                  if ($tpages > 8) {
                      $topic_pages .= " ... <a class='underline' href = 'topic.php?t=$list[topic_id]&page=$pages'>last page</a>";
                  }
                  $topic_pages .= ")";
              }
              
              if ($list['topic_icon'] != "" and $list['topic_icon'] != "0") {
                  $t_icon = "<img align='absmiddle' src='./img/misc/" . $list['topic_icon'] . "'>";
              } else {
                  $t_icon = "";
              }
              $pages = ceil(($list['reply']) / $post_per_page);
			  if ($pages == 0) {$pages += 1;}
              $last_post = $list['reply'];
              $last_post += 1;
              $topic_author = "<a href='user.php?u=" . $list['topic_by'] . "'>" . $list['topic_starter'] . "</a>";
              $tags = array('{F_ID}', '{T_ID}', '(TOPIC_SUBJECT)', '{TOPIC_AUTHOR}', '{REPLY}', '{VIEW}', '{T_DATE}', '{LAST_POSTER}', '{PAGE}', '{LAST_POST}', '{IMG_PATH}', '{TITLE}', '{SMALL_ICON}', '{PAGES}', '{TOPIC_ICON}','{VIEW_LATEST}','{BY}');
              $data = array($fid, $list['topic_id'], trim($list['topic_subject']), $topic_author, $list['reply'], $list['view'], date($date_format, strtotime($list['topic_last_post_date'])), $last_poster, $pages, $last_post, $topic_icon, $title_icon, $latest, $topic_pages, $t_icon,$l_view_latest,$l_by);
              echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/topics_row.html"));
              
              $counter += 1;
          }
      }
      echo "</tr></td></table>";
      if (mysql_num_rows($result2) > 0) {
          // BOTTOM PAGINATION
          echo "<table><tr><th></th></tr><tr>";
          $range = $max_pagination_link;
          if ($range >= $totalpages) {
              $range = $totalpages;
          }
          echo '<td align="right" class="pagination"> <b>' . $l_page . ' ' . $current_page . ' ' . $l_page_of . ' ' . $totalpages . '</b> [' . $numrows . ' posts]  &nbsp; &nbsp;';
          if ($currentpage > 1) {
              echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=1'><<</a> ";
              $prevpage = $currentpage - 1;
              echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$prevpage'><</a> ";
          }
          for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
              if (($x > 0) && ($x <= $totalpages)) {
                  if ($x == $currentpage) {
                      echo " [<b>$x</b>] ";
                  } else {
                      echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$x'>$x</a> ";
                  }
              }
          }
          if ($currentpage != $totalpages) {
              $nextpage = $currentpage + 1;
              echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$nextpage'>></a> ";
              echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$totalpages'>>></a> ";
          }
          echo '</td></tr></table> ';
      }
      // BOTTOM PAGINATION END
      echo '</table><br />' . $l_search_found . ' ' . $numrows . ' ' . $l_smatches . '';
      $counter = 0;
  }
  
  if ($hide_footer < 1) {
      include 'footer.php';
  }
  $pageContents = ob_get_contents();
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>